package com.sendpo.zjx.sendpo.activity;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lljjcoder.citypickerview.widget.CityPicker;
import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.net.HttpCode;
import com.sendpo.zjx.sendpo.net.HttpUrl;
import com.sendpo.zjx.sendpo.table.User;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.sendpo.zjx.sendpo.utils.Xutils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/8/4.
 */

public class LocEditManagerActivity extends AppCompatActivity {

    @BindView(R.id.head)
    RelativeLayout head;
    @BindView(R.id.realname_edit)
    EditText realnameEdit;
    @BindView(R.id.iphone_edit)
    EditText iphoneEdit;
    @BindView(R.id.cityaddress_edit)
    TextView cityaddressEdit;
    @BindView(R.id.address_edit)
    EditText addressEdit;

    private CityPicker cityPicker;
    public Dialog loadingdialog = null;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_locedit);
        Tools.initStatus(this, R.color.black);
        ButterKnife.bind(this);
        initComponent();

    }

    private void initComponent() {
        loadingdialog = new Dialog(this);
        loadingdialog.setContentView(R.layout.dialog_loading);
        loadingdialog.setCanceledOnTouchOutside(false);
        loadingdialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        cityPicker = new CityPicker.Builder(this)
                .textSize(20)
                .title("地址选择")
                .backgroundPop(0xa0000000)
                .titleBackgroundColor("#E6212A")
                .titleTextColor("#FFFFFF")
                .confirTextColor("#FFFFFF")
                .cancelTextColor("#FFFFFF")
                .province("广东省")
                .city("深圳市")
                .district("福田区")
                .textColor(Color.parseColor("#000000"))
                .provinceCyclic(true)
                .cityCyclic(false)
                .districtCyclic(false)
                .visibleItemsCount(7)
                .itemPadding(10)
                .onlyShowProvinceAndCity(false)
                .build();
        cityPicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
            @Override
            public void onSelected(String... contents) {
                cityaddressEdit.setText(contents[0] + contents[1] + contents[2]);
            }

            @Override
            public void onCancel() {

            }
        });
    }

    @OnClick(value = {R.id.backbt, R.id.cityaddress_edit, R.id.add_bt, R.id.add_bt1})
    public void onMyClick(View view) {
        switch (view.getId()) {
            case R.id.backbt:
                finish();
                break;
            case R.id.cityaddress_edit:
                cityPicker.show();
                break;
            case R.id.add_bt:
                if (TextUtils.isEmpty(realnameEdit.getText().toString().trim())) {
                    Toast.makeText(getApplicationContext(), "没有填写真实姓名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(iphoneEdit.getText().toString().trim())) {
                    Toast.makeText(getApplicationContext(), "没有填写手机号码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(cityaddressEdit.getText().toString().trim())) {
                    Toast.makeText(getApplicationContext(), "没有选择省城区", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(addressEdit.getText().toString().trim())) {
                    Toast.makeText(getApplicationContext(), "没有填写详细地址", Toast.LENGTH_SHORT).show();
                    return;
                }
                addAddress(realnameEdit.getText().toString().trim(),
                        iphoneEdit.getText().toString().trim(),
                        cityaddressEdit.getText().toString().trim(),
                        addressEdit.getText().toString().trim());
                break;
            case R.id.add_bt1:
                if (TextUtils.isEmpty(realnameEdit.getText().toString().trim())) {
                    Toast.makeText(getApplicationContext(), "没有填写真实姓名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(iphoneEdit.getText().toString().trim())) {
                    Toast.makeText(getApplicationContext(), "没有填写手机号码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(cityaddressEdit.getText().toString().trim())) {
                    Toast.makeText(getApplicationContext(), "没有选择省城区", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(addressEdit.getText().toString().trim())) {
                    Toast.makeText(getApplicationContext(), "没有填写详细地址", Toast.LENGTH_SHORT).show();
                    return;
                }
                addAddress(realnameEdit.getText().toString().trim(),
                        iphoneEdit.getText().toString().trim(),
                        cityaddressEdit.getText().toString().trim(),
                        addressEdit.getText().toString().trim());
                break;
        }
    }


    public void addAddress(String userConsignee,String userIphone,String userCity,String userDetailedAddress){
        loadingdialog.show();
        final List<User> userList = Xutils.queryAll(getApplication(), User.class);
        if (userList == null || userList.size() == 0) {
            return;
        }
        User user = userList.get(0);
        Map<String,String> map=new HashMap<>();
        map.put("userId",user.getUserId()+"");
        map.put("userConsignee",userConsignee);
        map.put("userIphone",userIphone);
        map.put("userCity",userCity);
        map.put("userDetailedAddress",userDetailedAddress);
        Xutils.POST(HttpUrl.useraddressadd, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                loadingdialog.dismiss();
                try {
                    JSONObject jb=new JSONObject(result);
                         int state=jb.getInt("state");
                    Toast.makeText(getApplicationContext(),jb.getString("message"),Toast.LENGTH_SHORT).show();
                 switch (state){
                     case HttpCode.SUCCESS:
                       finish();
                         break;
                        default:

                         break;
                 }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                loadingdialog.dismiss();
                Toast.makeText(getApplicationContext(),"网络异常",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }
}
